/*
 * @Author: humengchuan 531537052@qq.com
 * @Date: 2024-07-17 17:43:16
 * @LastEditors: humengchuan 531537052@qq.com
 * @LastEditTime: 2024-12-27 17:32:58
 * @FilePath: \projecte:\学习\react\react-optimize-ts\vite.config.ts
 * @Description:
 */
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";
import { visualizer } from "rollup-plugin-visualizer";

// https://vitejs.dev/config/
export default defineConfig(() => {
  return {
    plugins: [
      react(),
      visualizer({
        // gzipSize: true,
        // brotliSize: true,
        open: true,
        emitFile: true,
        filename: "analyzing.html",
      }),
    ],
    resolve: {
      alias: {
        "~": path.resolve(__dirname, "./"),
        "@": path.resolve(__dirname, "./src"),
      },
    },
    server: {
      host: "0.0.0.0",
      port: 8080,
      proxy: {
        "/api": {
          target: "http://localhost:3000",
          rewrite: (path) => path.replace(/^\/api/, ""),
          changeOrigin: true, // 允许跨域
          logLevel: "debug",
        },
        "/v1": {
          target: "http://localhost:3008",
          rewrite: (path) => path.replace(/^\/api/, ""),
          changeOrigin: true, // 允许跨域
          logLevel: "debug",
        },
        "/graphql": {
          target: "ws://localhost:3000",
          changeOrigin: true, // 允许跨域
        },
      },
    },
    build: {
      cssCodeSplit: true,
      chunkSizeWarningLimit: 2000,
      assetsInlineLimit: 4000,
      sourcemap: false,
      minify: "terser", // 代码混淆
      assetsDir: "static/assets",
      // 在生产环境移除console.log
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
    },
  };
});
